package game.module.itemnew.logic;

import game.constants.Position;
import game.module.itemnew.beans.ItemStorage.Item;
import game.module.itemnew.dao.ItemCache;
import game.module.itemnew.dao.ItemTemplateCache;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ItemManager {

	@Autowired
	private ItemCache itemCache;

	@Autowired
	private ItemTemplateCache itemTemplateCache;

	/**
	 * 背包剩余空格数
	 * 
	 * @param heroId
	 * @return
	 */
	public short getBagFreeCellCount(int heroId, int bagCapacity) {
		Map<Short, Item> bagItems = itemCache.getBagGoodsMap(heroId);
		short freeCount = 0;
		for (short i = Position.BagGoodsBeginPostion; i <= Position.BagGoodsBeginPostion + bagCapacity; i++) {
			if (bagItems.get(i) == null) {
				freeCount++;
			}
		}
		return freeCount;
	}

	/**
	 * 第一个空的格子
	 * 
	 * @param heroId
	 * @param bagCapacity
	 * @return
	 */
	public short getFirstFreeCell(int heroId, int bagCapacity) {
		short ret = 0;
		Map<Short, Item> bagItems = itemCache.getBagGoodsMap(heroId);
		for (short i = Position.BagGoodsBeginPostion; i <= bagCapacity; i++) {
			if (bagItems.get(i) == null) {
				ret = i;
				break;
			}
		}
		return ret;
	}

}
